﻿/*----------------------------------------------------------------
// Copyright (C) 2011 
// 版权所有。 
//
// 文件名：RegexTool.cs
// 文件功能描述：
//
// 
// 创建标识：kenter 2011-10-19
// EMAIL:kenter1643@163.com
//
// 修改标识：
// 修改描述：
//
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WeiBoUI.Template
{
    public class RegexTool
    {
        public static string GetContentByRegex(string result, string leftCut, string rightCut, bool delHtml)
        {
            string pattern = leftCut + "(?<TagName>[\\s\\S]*?)" + rightCut;
            Match match = Regex.Match(result, pattern, RegexOptions.Multiline);
            if (match.Success)
            {
                string content = match.Result("${TagName}");
                if (delHtml)
                {
                    content = Regex.Replace(content, @"<[\s\S]*?>", string.Empty, RegexOptions.IgnoreCase);
                    content = Regex.Replace(content, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
                    content = Regex.Replace(content, @"&(amp|#38);", "", RegexOptions.IgnoreCase);  
                    content = Regex.Replace(content, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
                    content = Regex.Replace(content, "▶", "", RegexOptions.IgnoreCase);
                    content = Regex.Replace(content, "▋", "", RegexOptions.IgnoreCase);
                    content = Regex.Replace(content, "&#183;", "", RegexOptions.IgnoreCase);
                    content = Regex.Replace(content, "$", "", RegexOptions.IgnoreCase);
                    content = Regex.Replace(content, "#", "", RegexOptions.IgnoreCase);
                    content = Regex.Replace(content, "◤", "", RegexOptions.IgnoreCase);
                    content = Regex.Replace(content, "◀", "", RegexOptions.IgnoreCase);
                        
                    content = content.Replace("<BR>", @"\r\n");
                }

                return content;
            }
            return "";
        }

        public static List<string> GetContentHrefs(string result, string baseUrl, string[] tags = null)
        {
            bool isContantTag = false;
            List<string> hrefs = new List<string>();
            MatchCollection mc = Regex.Matches(result, "(?<=[\\s+]?href[\\s+]?=[\\s+]?('|\")?)[^(\"|')>]+?(?=\"|')"
                , RegexOptions.IgnoreCase | RegexOptions.Compiled);
            foreach (Match ma in mc)
            {
                string href = ma.Value;

                if (tags != null)
                {
                    foreach (string tag in tags)
                    {
                        if (href.Contains(tag))
                        {
                            isContantTag = true;
                            goto CheckHttp;
                        }
                    }
                }
                else
                {
                    isContantTag = true;
                }

            CheckHttp:
                if (!href.Contains("http"))
                {
                    href = baseUrl + href;
                }

                if (isContantTag)
                {
                    hrefs.Add(href);
                }


                isContantTag = false;
            }
            return hrefs;
        }
    }
}
